package com.lz.sports.dialog

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.click
import com.lz.sports.R
import com.lz.sports.bean.DeviceInfo
import com.lz.sports.databinding.DialogDeviceNumBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//序号编辑
class DeviceNumDialog : BaseDialogFragment<DialogDeviceNumBinding>() {

    companion object {
        fun show(fragment: Fragment, position: Int, deviceInfo: DeviceInfo): DeviceNumDialog {
            val dialog = DeviceNumDialog().also {
                it.arguments = Bundle().apply {
                    putInt("position", position)
                    putParcelable("deviceInfo", deviceInfo)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }

        fun show(activity: FragmentActivity, position: Int, deviceInfo: DeviceInfo): DeviceNumDialog {
            val dialog = DeviceNumDialog().also {
                it.arguments = Bundle().apply {
                    putInt("position", position)
                    putParcelable("deviceInfo", deviceInfo)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }
    }

    override fun getLayoutRes(): Int = R.layout.dialog_device_num

    override fun initData(v: View, savedState: Bundle?) {
        lifecycleScope.launch {
            arrayOf(binding.tvConfirm, binding.tvCancel).click().collect {
                when (it.id) {
                    R.id.tv_cancel -> dismiss()
                    R.id.tv_confirm -> confirm()
                }
            }
        }

        binding.etInput.addTextChangedListener(object : TextWatcher {

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit

            override fun afterTextChanged(s: Editable?) {
                binding.tvConfirm.isEnabled = s.toString().trim().toIntOrNull() != null
            }
        })

        binding.device = arguments!!.getParcelable("deviceInfo")
    }

    private fun confirm() {
        val num = binding.etInput.text.toString().trim().toIntOrNull() ?: return
        val position = arguments!!.getInt("position")
        if (isFromActivity()) {
            if (context is OnNumEditListener) {
                (context as OnNumEditListener).onNumEdit(this, num, position)
            }
        } else {
            if (parentFragment is OnNumEditListener) {
                (parentFragment as OnNumEditListener).onNumEdit(this, num, position)
            }
        }
    }

    interface OnNumEditListener {

        fun onNumEdit(dialog: DeviceNumDialog, number: Int, position: Int)
    }
}